package com.atguigu.commonutils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

public class JwtUtils {


    /**
     * EXPIRE过期时间
     * APP_SECRET 密钥
     */
    public static final long EXPIRE = 1000 * 60 * 60 * 24;
    public static final String APP_SECRET = "jasonxjc78963214xjc";
    //生成token字符串方法
    public static String getJwtToken(String id, String nickname)
    {
        String JwtToken = Jwts.builder()
                .setHeaderParam("typ", "JWT")
                .setHeaderParam("alg", "HS256")
                .setSubject("guli-user")
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRE))
                .claim("id", id)
                .claim("nickname", nickname)
                .signWith(SignatureAlgorithm.HS256, APP_SECRET)
                .compact();
        return JwtToken;
    }
    /**
     *判断token是否存在与有效
     * 校验token
     *@param jwtToken
     *@return     */
    public static boolean checkToken(String jwtToken) {
        if(StringUtils.isEmpty(jwtToken)) {
            return false;
        }
        try {
        Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(jwtToken);
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
        return true;
    }
    /**
     *判断token是否存在与有效
     *@param request
     *@return     */
    public static boolean checkToken(HttpServletRequest request) {
        try {
            //从请求中获取请求头
        String jwtToken = request.getHeader("token");
        if(StringUtils.isEmpty(jwtToken)) {
            return false;
        }
        //校验
        Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(jwtToken);
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
        return true;     }
    /**
     *根据token获取会员id
     *@param request
     *@return     */
    public static String getMemberIdByJwtToken(HttpServletRequest request) {
        String jwtToken = request.getHeader("token");
        if(StringUtils.isEmpty(jwtToken)){
            return "";
        }
        //校验token
        Jws<Claims> claimsJws = Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(jwtToken);
        Claims claims = claimsJws.getBody();
        return (String)claims.get("id");
    }


}
